package com.zlkj.pro.core.security;

import cn.stylefeng.roses.kernel.auth.api.exception.AuthException;
import cn.stylefeng.roses.kernel.scanner.api.pojo.resource.ResourceDefinition;
import cn.stylefeng.roses.kernel.system.modular.user.entity.SysUser;
import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zlkj.pro.core.exception.enums.BusinessExceptionEnum;
import com.zlkj.pro.core.security.base.BaseSecurityInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 鉴权的过滤器，用来鉴权token
 *
 * @author fengshuonan
 * @date 2020/12/15 22:45
 */
@Component
@Slf4j
public class AppUserSecurityInterceptor extends BaseSecurityInterceptor {

    @Autowired
    private SysUserService sysUserService;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // 获取请求的URI
        log.info("请求URI：{}", request.getRequestURI());
        // 获取header中的userId
        log.info("请求userId：{}", request.getHeader("User-Id"));
        String userId = request.getHeader("User-Id");
        if (StringUtils.isNotBlank(userId)) {
            SysUser user = sysUserService.getById(userId);
            if (user.getStatusFlag() == 2) {
                throw new AuthException(BusinessExceptionEnum.USER_STATUS_FAIL);
            }
        }
        return true;
    }

    @Override
    public void filterAction(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, ResourceDefinition resourceDefinition, String token, Object handler) {

    }

}
